﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;

[System.Serializable]
public class FollowObject
{
    public Transform _effect;
    public Transform _dolly;
    public Transform _followObj;
}

public class MainController : MonoSingleton<MainController> {

    public ProcessMusic _musicCore;
    public Camera _cam;
    public Gradient _gradientColor;
    public CinemachineVirtualCamera _v_cam;
    public List<FollowObject> _effects = new List<FollowObject>();
    FollowObject _currentEffect;
    int index;
    SettingData _settingdata;

    protected override void Init()
    {
        base.Init();
        MainWindow.Instance.gameObject.SetActive(false);
       _settingdata = SettingManager.Instance._settingData;
        _musicCore = GetComponent<ProcessMusic>();
        _cam = Camera.main;
        InvokeRepeating("RandomChangeEffect", 60f, Random.Range(_settingdata._effect_changeDur_min, _settingdata._effect_changeDur_max));
        InvokeRepeating("RandomChangeCameraMode", 120, Random.Range(_settingdata._cameraMode_changeDur_min, _settingdata._cameraMode_changeDur_max));
        index = _settingdata._currentEffectIndex;
        _currentEffect = _effects[index];

        ChangeTheEffect(index);
    }

    // Update is called once per frame
    void Update () {
        _cam.backgroundColor = _gradientColor.Evaluate(_musicCore.ampMultiplier / 2f);
        if (Input.GetKeyDown(KeyCode.F))
            RandomChangeCamClearflag();
        if (Input.GetKeyDown(KeyCode.G))
            RandomChangeEffect();
        if (Input.GetKeyDown(KeyCode.F1))
            MainWindow.Instance.gameObject.SetActive(!MainWindow.Instance.gameObject.activeInHierarchy);
        if (Input.GetKeyDown(KeyCode.C))
            PlayerPrefs.DeleteAll();
    }

    public void ChangeClearFlag(CameraClearFlags clearflag)
    {
        _cam.clearFlags = clearflag;
        _settingdata._cam_mode = clearflag == CameraClearFlags.Nothing?CameraMode.Paint:CameraMode.Normal;
    }

    public void ChangeTheEffect(int index)
    {
        this.index = index;
        _settingdata._currentEffectIndex = index;
        EffectChange();
    }

    private void EffectChange()
    {
        foreach (var item in _effects)
        {
            item._dolly.gameObject.SetActive(false);
            item._effect.gameObject.SetActive(false);
        }

        _currentEffect = _effects[index];
        _currentEffect._dolly.gameObject.SetActive(true);
        _currentEffect._effect.gameObject.SetActive(true);
        _v_cam.LookAt = _currentEffect._effect;
        _v_cam.m_Follow = _currentEffect._followObj;
    }


    void RandomChangeEffect()
    {
        if (_settingdata._effect_mode == EffectMode.Random)
        {
            index = Random.Range(0, _effects.Count);
            EffectChange();
        }
    }

    void RandomChangeCameraMode()
    {
        if (_settingdata._effect_mode == EffectMode.Random)
        {
            RandomChangeCamClearflag();
        }
    }

    void RandomChangeCamClearflag()
    {
        _cam.clearFlags = _cam.clearFlags == CameraClearFlags.Nothing ? CameraClearFlags.SolidColor : CameraClearFlags.Nothing;
    }
}
